layui.use(['jquery','layer','form'],function() {
    var jquery = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    //页面赋值
    var product  = JSON.parse(localStorage.getItem("product"));
    var productId = product.productId;
    setTable();
    function setTable(){
        $('#productCode').text(product.productCode);
        $('#productName').text(product.productName);
        $('#currentpriceProduct').text(product.currentpriceProduct);
        $('#categoryTitle').text(product.title);
        $('#productLabel').text(product.productLabel);
        $('#keywords').text(product.searchKeywords);
        $('#createName').text(product.createName);
        $('#createDate').text(product.createDate);
        $('#picture').attr('src',product.mainImage);
        $('#description').text(product.productDescription);
        var result = product.result;
        if(result == 1){
            $('#result').text('审核通过');
        }else if(result == 2){
            $('#result').text('待审核');
        }else if(result == 3){
            $('#result').text('未通过');
        }
    }
    //用户赋值
    var user = JSON.parse(localStorage.getItem('user'));
    $('input[name=trueName]').val(user.data.trueName);
    $('input[name=account]').val(user.data.account);
    setTimeout(getAllComment,200);
    function getAllComment() {
        $.ajax({
            url: '/comment/getAllCourseComments',
            dataType: 'json',
            data:{
                commodityId: productId,
                type: 3
            },
            success: function (data) {
                var comment = data.data;
                for (var i = 0; i < comment.length; i++) {
                    if (comment[i].display == 2 && comment[i].status == 1) {
                        $('.all_body').append('<div id="all_item">' + '<img id="photo" src="' + comment[i].imageUrl + '"/>&nbsp;' +
                            '<label id="comment_user">' + comment[i].account + '：' + '</label>&nbsp;' +
                            '<label id="comment_content">' + comment[i].content + '</label>&nbsp;&nbsp;&nbsp;' +
                            '<label id="show_time">' + comment[i].commentDate + '</label>&nbsp;&nbsp;&nbsp;' +
                            '<a onclick="javascript:hideComment(' + comment[i].id + ',' + comment[i].display + ');">隐藏</a>&nbsp;&nbsp;&nbsp;' +
                            '<a onclick="javascript:deleteComment(' + comment[i].id + ');">删除</a>' + '</div>');
                    } else if (comment[i].display == 3 && comment[i].status == 1) {
                        $('.all_body').append('<div id="all_item">' + '<img id="photo" src="' + comment[i].imageUrl + '"/>&nbsp;' +
                            '<label id="comment_user">' + comment[i].account + '：' + '</label>&nbsp;' +
                            '<label id="comment_content">' + comment[i].content + '</label>&nbsp;&nbsp;&nbsp;' +
                            '<label id="show_time">' + comment[i].commentDate + '</label>&nbsp;&nbsp;&nbsp;' +
                            '<a onclick="javascript:hideComment(' + comment[i].id + ',' + comment[i].display + ');">显示</a>&nbsp;&nbsp;&nbsp;' +
                            '<a onclick="javascript:deleteComment(' + comment[i].id + ');">删除</a>' + '</div>');
                    } else if (comment[i].status == 3) {
                        $('.all_body').append('<div id="all_item">' + '<img id="photo" src="' + comment[i].imageUrl + '"/>&nbsp;' +
                            '<label id="comment_user">' + comment[i].account + '：' + '</label>&nbsp;' +
                            '<label id="comment_content">' + comment[i].content + '</label>&nbsp;&nbsp;&nbsp;' +
                            '<label id="show_time">' + comment[i].commentDate + '</label>&nbsp;&nbsp;&nbsp;' +
                            '<label style="color: #037EFE">已删除</label>' + '</div>');
                    }

                }
            }
        });
    }
    //根据评论id更改显示状态
    hideComment = function hideComment(id,display) {
        display = displayTransFer(display);
        $.ajax({
            url: '/comment/updateCommentDisplayById',
            type: 'post',
            data: {
                id: id,
                display: display,
                type: 3
            },
            success: function (data) {
                if(data.data){
                    if(display == 2){
                        layer.msg('评论显示成功');
                    }else if(display == 3){
                        layer.msg('评论隐藏成功');
                    }
                    $('.all_body').children().remove();
                    getAllComment();
                }
            }
        });
    }
    //删除评论（假删除）
    deleteComment = function deleteComment(id) {
        $.ajax({
            url: '/comment/updateCommentStatusById',
            type: 'post',
            data: {
                id: id,
                type: 3,
                status: 3
            },
            success: function (data) {
                if(data.data){
                    layer.msg('评论删除成功');
                    $('.all_body').children().remove();
                    getAllComment();
                }
            }
        })
    }
    function displayTransFer(display){
        if(display == 2){
            return 3;
        }else if(display == 3){
            return 2;
        }
    }
    //发表评论
    form.on('submit(announce)',function (obj) {
        var user = JSON.parse(localStorage.getItem('user'));
        $.ajax({
            url: '/comment/addComment',
            dataType: 'json',
            type: 'post',
            data: {
                userId: user.data.userId,
                commodityId: productId,
                type: 3,
                content: obj.field.content,
                parentId: 0,
                display: 2,
                fabulous: 0,
                reply: 0,
                createId: user.data.userId,
                status: 1,
                userType: 2
            },
            success: function (data) {
                if(data.data.id != null){
                    layer.msg('发表评论成功')
                    $('.all_body').children().remove();
                    getAllComment();
                    $('#content').val('');
                }
            }
        });
        return false;
    });
})